﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;
using System.Data;
namespace Kingdee.Bos.Project.DyForm
{
    [Description("【服务插件】新增物料，审核后添加价格到对应物料表")]
    [HotUpdate]

    public class YongGuWuLiaoShenHe : AbstractOperationServicePlugIn
    {


        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            // 添加预加载字段
            e.FieldKeys.Add("Number");
        }
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            foreach (DynamicObject item in e.DataEntitys)
            {
                var FNUMBER = item["Number"].ToString();//编号
                base.EndOperationTransaction(e);
                //根据有当前编号获取价格、价目表信息
                String SQLSelectJGZD = "select F_UIXC_JGZD1, F_UIXC_JGZD2, F_UIXC_JGZD3, F_UIXC_JGZD4, F_UIXC_JGZD5" +
                    ", F_UIXC_JGZD6, F_UIXC_JGZD7, F_UIXC_JGZD8, F_UIXC_JGZD9, F_UIXC_JGZD10 from T_BD_MATERIAL  WHERE FNUMBER='" + FNUMBER + "'";
                DataTable dtJGZD;
                dtJGZD = DBUtils.ExecuteDataSet(this.Context, SQLSelectJGZD).Tables[0];

                String SQLSelectJMB = "select F_UIXC_JMB1,F_UIXC_JMB2,F_UIXC_JMB3,F_UIXC_JMB4,F_UIXC_JMB5" +
                    ",F_UIXC_JMB6,F_UIXC_JMB7,F_UIXC_JMB8,F_UIXC_JMB9,F_UIXC_JMB10 from T_BD_MATERIAL WHERE FNUMBER='" + FNUMBER + "'";
                DataTable dtJMB;
                dtJMB = DBUtils.ExecuteDataSet(this.Context, SQLSelectJMB).Tables[0];

                //获取物料....
                String SQLSelectWL = "select T1.FMATERIALID AS 物料ID,FAPPROVEDATE AS 审核日期, FNUMBER AS 物料编码,T2.FNAME AS 物料名称,FSPECIFICATION AS 规格型号,T4.FNAME AS 计价单位,FUNITID " +
                    "FROM T_BD_MATERIAL T1 LEFT JOIN T_BD_MATERIAL_L T2 " +
                    "ON T2.FMATERIALID=T1.FMATERIALID LEFT JOIN t_BD_MaterialSale T3 " +
                    "ON T3.FMATERIALID=T1.FMATERIALID LEFT JOIN T_BD_UNIT_L T4 " +
                    "ON T4.FUNITID=T3.FSALEPRICEUNITID WHERE FNUMBER='" + FNUMBER + "'";
                DataTable dtWL;
                dtWL = DBUtils.ExecuteDataSet(this.Context, SQLSelectWL).Tables[0];

                string WLBMID = dtWL.Rows[0][0].ToString();

                //修改文本：价格信息锁定性判断值 为 1
                string SQLupdate = "update T_BD_MATERIAL set F_VHTP_Integer=1 where FMATERIALID='" + WLBMID + "' ";
                DBUtils.ExecuteDynamicObject(this.Context, SQLupdate);


           


                int count = dtJGZD.Rows.Count;
                //价格、价目表各有10个价格字段
                for (int i = 1; i <= 10; i++)
                {
                    // 价格：  dbJG
                    string JG = dtJGZD.Rows[0][i - 1].ToString();
                    double dbJG = double.Parse(JG);


                    //销售价目表 FID :XSJMBFID  
                    string XSJMBFID = dtJMB.Rows[0][i - 1].ToString();

                    // 物料编码ID FMATERIALID：WLBMID


                    //判断是否存在重复价目表
                    int a = 0;
                    int b = 0;
                    int PanDuanZhi2 = 0;

                    for (a = 0; a < 9; a++)
                    {
                        for (b = a + 1; b < 10; b++)
                        {
                            string q = dtJMB.Rows[0][a].ToString();
                            string w = dtJMB.Rows[0][b].ToString();
                            if (dtJMB.Rows[0][a].ToString() == dtJMB.Rows[0][b].ToString() && dtJMB.Rows[0][a].ToString() != "0" && dtJMB.Rows[0][b].ToString() != "0")
                            {
                                PanDuanZhi2 = 1;
                            }

                        }
                    }
                    if (PanDuanZhi2 == 1)
                    {

                        throw new KDBusinessException("", "该物料输入重复价目表！");
                    }
                    else
                    {
                        //重复判断
                        //通过 销售价目表FID 物料编码ID  判断之前价目表是否已经存在该物料
                        String SQLChongFuPanDuan = "select * from T_SAL_PRICELISTENTRY T1 " +
                            "LEFT JOIN T_SAL_PRICELIST T2 ON T2.FID=T1.FID " +
                            "  where T2.FID='" + XSJMBFID + "' AND  FMATERIALID='" + WLBMID + "'";
                        DataTable dtChongFuPanDuan;
                        dtChongFuPanDuan = DBUtils.ExecuteDataSet(this.Context, SQLChongFuPanDuan).Tables[0];

                        if (dtChongFuPanDuan.Rows.Count > 0)
                        {

                        }
                        else
                        {
                            if (dbJG > 0 && XSJMBFID != "0")
                            {

                                //将对应的价格字段的价格，新增到价目表
                                // DynamicObjectCollection Entrys = this.Model.DataObject["F_GSCY_Entity"] as DynamicObjectCollection;

                                // 单位ID FUNITID：FUNITID
                                string WLFUNITID = dtWL.Rows[0][6].ToString();

                                //价格系数 ：JGXS
                                double JGXS = 1;
                                //最低限价 ：ZDXJ
                                double ZDXJ = dbJG;
                                //生效日期 : EffectiveDate
                                string EffectiveDate = DateTime.Now.Date.ToString();
                                //失效日期 :  ExpirationDate
                                string ExpirationDate = "21000101";
                                //失效 :  SX
                                string SX = "A";
                                //行审核状态 :  HSHZT
                                string HSHZT = "A";


                                //获取销售价目表价格明细 FSEQ     FMATERIALTYPEID                   
                                string SQLSelectFSEQ = "SELECT FSEQ ,FMATERIALTYPEID FROM T_SAL_PRICELISTENTRY where FID='" + XSJMBFID + "' ORDER BY FSEQ DESC ";
                                DataTable dtFSEQ;
                                dtFSEQ = DBUtils.ExecuteDataSet(this.Context, SQLSelectFSEQ).Tables[0];
                                string FSEQ = dtFSEQ.Rows[0][0].ToString();

                                string FMATERIALTYPEID = dtFSEQ.Rows[0][1].ToString();

                                string newFSEQ = (double.Parse(FSEQ) + 1).ToString();

                                //产生系统表格流水号 
                                //避免 新增单据产生的FENTRYID 与 之前直接添加 FENTRYID 冲突
                                string insertFENTRYID = "insert into z_SAL_PRICELISTENTRY (column1) values (1) ";
                                DBUtils.ExecuteDynamicObject(this.Context, insertFENTRYID);

                                //获取 FENTRYID
                                string SQLFENTRYID = "select id from z_SAL_PRICELISTENTRY ";
                                DataTable dtFENTRYID;
                                dtFENTRYID = DBUtils.ExecuteDataSet(this.Context, SQLFENTRYID).Tables[0];

                                int dtFENTRYIDcount = dtFENTRYID.Rows.Count;
                                string newFENTRYID = dtFENTRYID.Rows[dtFENTRYIDcount - 1][0].ToString();

                                //删除系统表格产生流水号 
                                string deleteFENTRYID = "delete from z_SAL_PRICELISTENTRY ";
                                DBUtils.ExecuteDynamicObject(this.Context, deleteFENTRYID);

                                //添加对应数据
                                string SQLinsert = "insert into T_SAL_PRICELISTENTRY (FENTRYID,FID,FSEQ,FMATERIALTYPEID,FMATERIALID,FPRICEUNITID,FPRICEBASE,FPRICE,FDOWNPRICE,FEFFECTIVEDATE,FEXPRIYDATE,FFORBIDSTATUS,FROWAUDITSTATUS,FUNITID)  " +
                                    "values('" + newFENTRYID + "', '" + XSJMBFID + "', '" + newFSEQ + "', '" + FMATERIALTYPEID + "','" + WLBMID + "'," +
                                    "'" + WLFUNITID + "','" + JGXS + "','" + JG + "','" + ZDXJ + "','" + EffectiveDate + "','" + ExpirationDate + "','" + SX + "','" + HSHZT + "','" + WLFUNITID + "') ";
                                DBUtils.ExecuteDynamicObject(this.Context, SQLinsert);

                            }
                            else  if (dbJG > 0 && XSJMBFID == "0")
                            {

                                throw new KDBusinessException("", "该物料存在价格不为0，但对应价目表为空的情况！");

                            }
                        }

                    }
                }

            }
        }

    }

}

